﻿using System.Collections.Generic;
using UnityEngine;
using System;

namespace TC
{
    [Serializable]
    [JsonPolymorphism(typeof(Context))]
    public class Context
    {
        public Dictionary<string, object> variableDic = new Dictionary<string, object>();

        //public void AddVariable(string key, int value)
        //{
        //    AddVariableObj(key, value);
        //}
        
        //public void AddVariable(string key, float value)
        //{
        //    AddVariableObj(key, value);
        //}
        
        //public void AddVariable(string key, double value)
        //{
        //    AddVariableObj(key, value);
        //}
        
        //public void AddVariable(string key, bool value)
        //{
        //    AddVariableObj(key, value);
        //}
        
        //public void AddVariable(string key, string value)
        //{
        //    AddVariableObj(key, value);
        //}

        //public void AddVariable(string key, Vector2 value)
        //{
        //    string str = $"{value.x},{value.y}";
        //    AddVariableObj(key, str);
        //}

        //public void AddVariable(string key, Vector3 value)
        //{
        //    string str = $"{value.x},{value.y},{value.z}";
        //    AddVariableObj(key, str);
        //}

        //public void AddVariable(string key, Vector4 value)
        //{
        //    string str = $"{value.x},{value.y},{value.z},{value.w}";
        //    AddVariableObj(key, str);
        //}

        public void AddVariable(string key, object value)
        {
            if (value == null) return;
            
            if (variableDic.ContainsKey(key))
            {
                variableDic[key] = value;
            }
            else
            {
                variableDic.Add(key, value);
            }
        }

        public void Remove(string key)
        {
            variableDic.Remove(key);
        }

        public T Get<T>(string key)
        {
            if(variableDic.ContainsKey(key))
            {
                return (T)variableDic[key];
            }
            return default;
        }

        public string ToJSON()
        {
            foreach (var item in variableDic)
            {
                Debug.Log(item.Key + " " + item.Value);
            }
            Debug.Log("===========================");
            Debug.Log(JsonUtility.ToJson(variableDic));
            Debug.Log("***************************");
            return JsonUtility.ToJson(variableDic);
        }
    }
}
